//
//  DayPointView.m
//  Eath
//
//  Created by yuhaya on 10/5/14.
//  Copyright (c) 2014 yuhaya. All rights reserved.
//

#import "DayPointView.h"
#import "Tool.h"
//#import "Pointer.h"

const int x1 = 50;
const int x2 = 300;
const int t1 = 6*60;
const int t2 = 24*60;

@implementation DayPointView

- (id)initWithFrame:(CGRect)frame andData:(NSDictionary *)data
{
    self = [super initWithFrame:frame];
    if (self) {
        //        self.backgroundColor = [UIColor whiteColor];
        self.opaque = NO;
        self.DayDate = data;
    }
    return self;
}

- (void)drawRect:(CGRect)rect {
    // Drawing code
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetRGBFillColor (context,  0.000, 0.690, 0.906, 1.0);//设置填充颜色
    UIFont  *font = [UIFont boldSystemFontOfSize:12.0];//设置
    DLog(@"%@",self.DayDate[@"week"]);
    [self.DayDate[@"week"] drawInRect:CGRectMake(12, 10, 80, 20) withFont:font];
    NSString * timestamp = self.DayDate[@"time"];
    NSDate * time = [NSDate dateWithTimeIntervalSince1970:[timestamp integerValue]];
    NSString * time_str = [Tool getDateStringWithDate:time DateFormat:@"MM-dd"];
    [time_str drawInRect:CGRectMake(10, 30, 80, 20) withAttributes:@{NSFontAttributeName:font}];
    
    CGPoint point_start = CGPointMake(x1, 30);
    CGPoint point_end = CGPointMake(x2, 30);
    CGContextSetLineWidth(context,2);
    CGContextSetRGBStrokeColor(context, 0.749, 0.749, 0.749, 1);
    CGContextMoveToPoint(context, point_start.x, point_start.y);
    CGContextAddLineToPoint(context, point_end.x, point_end.y);
    CGContextClosePath(context);
    CGContextStrokePath(context);
    
    NSMutableArray * point_arr = self.DayDate[@"point"];
    NSMutableArray * point_position = [NSMutableArray array];
    for (int a = 0; a < point_arr.count; a++) {
        NSString * num = point_arr[a];
        int tp = [[Tool time_str_num:num] intValue];
        int xp = (x2 - x1)*(tp-t1)/(t2 - t1)+x1;
        point_position[a] = [NSNumber numberWithInt:xp];
    }
    
    //画点
    UIColor*aColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:1];
    CGContextSetLineWidth(context, 2.0);//线的宽度
    UIFont * font2 = [UIFont boldSystemFontOfSize:12.0];//设置字体大小
    
    Boolean flag = YES;
    
    for (int num = 0; num < point_position.count; num ++) {
        CGContextSetRGBStrokeColor(context, 0.110, 0.573, 0.980, 1);
        CGContextAddArc(context, [point_position[num] intValue], 30, 3, 0, 2*PI, 0); //添加一个圆
        CGContextSetFillColorWithColor(context, aColor.CGColor);//填充颜色
        CGContextDrawPath(context, kCGPathFillStroke); //绘制路径加填充
        
        CGContextSetRGBFillColor (context,  0.467, 0.467, 0.565, 1.0);
        if (flag) {
            [point_arr[num] drawInRect:CGRectMake([point_position[num] intValue]-16, 40, 40, 20) withFont:font2];
            flag = NO;
        }else{
            [point_arr[num] drawInRect:CGRectMake([point_position[num] intValue]-16, 5, 40, 20) withFont:font2];
            flag = YES;
        }
        
    }
    
}

@end
